package com.example.springjpa.controller;


import com.example.springjpa.pojo.User;
import com.example.springjpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;


/**
 * @author 杰哥
 * @Title: UserController
 * @ProjectName springjpa
 * @Description: TODO
 * @date 2019/8/819:21
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("select")
    public ModelAndView hello() {
        ModelAndView model = new ModelAndView("index");
        List<User> byAll = userService.findByAll();
        model.addObject("userList", byAll);
        return model;
    }

    /**
     * 添加
     * @param username
     * @param password
     * @return 1 成功 2 失败
     */
    @GetMapping("insert")
    public Integer insert(String username, String password,String role) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(role);
        User save = userService.insert(user);
        if (save != null) {
            return 1;
        } else {
            return 0;
        }

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete")
    public Integer delete(Integer id) {

        return userService.delete(id);

    }

    /**
     * 更新
     * @return 0更新失败 1更新成功  2 用户不存在
     */
    @GetMapping("update")
    public Integer update(Integer id,String username, String password,String role) {
        User byId = userService.findById(id);
        if(byId == null){
            return 2;
        }
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(role);
        User update = userService.update(user);
        if(update != null){
            return 1;
        }else {
            return 0;
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("findById")
    public User findById(Integer id) {
        return userService.findById(id);
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return 1 成功 2 密码错误 0 失败
     */
    @GetMapping("login")
    public Integer login(String username, String password) {
        try {
            List<User> byName = userService.findByName(username);
            if (byName != null && !byName.isEmpty()) {
                User user = byName.get(0);
                if (user.getPassword().equals(password)) {

                    return 1;
                } else {
                    return 2;
                }
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    @GetMapping("show")
    public ModelAndView show(){
        ModelAndView model = new ModelAndView("show");
        model.addObject("id","28");
        model.addObject("username","jiege");
        return model;
    }
    @RequestMapping(value = "/getMessage", method = RequestMethod.GET)
    public ModelAndView getMessage() {
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("message","欢迎进入，您的身份是用户");
        return modelAndView;
    }
}
